package com.ruijie.autotest.log.common.sql;

import java.sql.ResultSet;
import java.sql.SQLException;

public class SqlType {
	private final String typeAsText;

	public SqlType(String typeAsText) {
		this.typeAsText = typeAsText;
	}

	public String toTypeText() {
		return typeAsText;
	}

	public String represent(String actualValue) {
		// FIXME 2021/1/7  wait for me!!!     convert to Types
		if (typeAsText.contains("varchar")) {
			return "'" + actualValue + "'";
		} else if (typeAsText.contains("int") || typeAsText.contains("float")) {
			return actualValue;
		} else if (typeAsText.contains("timestamp") || typeAsText.contains("datetime")) {
			return "DATE_FORMAT('" + actualValue + "', '%Y-%m-%d %H:%i:%S')";
		}
		throw new RuntimeException("not a supported type: " + typeAsText);
	}

	public String valueFrom(ResultSet resultSet) throws SQLException {
		if (typeAsText.contains("varchar")) {
			return resultSet.getString(1);
		} else if (typeAsText.contains("timestamp") || typeAsText.contains("datetime")) {
			// FIXME 2021/1/12  wait for me!!!
			return String.valueOf(resultSet);
		} else if (typeAsText.contains("int")) {
			return String.valueOf(resultSet.getLong(1));
		}
		throw new RuntimeException("not a supported type: " + typeAsText);
	}
}
